package cn.iocoder.yudao.module.game.service.gameList;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.game.controller.admin.gameList.vo.GameListPageReqVO;
import cn.iocoder.yudao.module.game.controller.admin.gameList.vo.GameListSaveReqVO;
import cn.iocoder.yudao.module.game.dal.dataobject.gameList.GameListDO;
import cn.iocoder.yudao.module.game.dal.mysql.gameList.GameListMapper;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.game.enums.ErrorCodeConstants.LIST_NOT_EXISTS;

/**
 * 游戏列表 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class GameListServiceImpl implements GameListService {

    @Resource
    private GameListMapper listMapper;

    @Override
    public Long createList(GameListSaveReqVO createReqVO) {
        // 插入
        GameListDO list = BeanUtils.toBean(createReqVO, GameListDO.class);
        listMapper.insert(list);
        // 返回
        return list.getId();
    }

    @Override
    public void updateList(GameListSaveReqVO updateReqVO) {
        // 校验存在
        validateListExists(updateReqVO.getId());
        // 更新
        GameListDO updateObj = BeanUtils.toBean(updateReqVO, GameListDO.class);
        listMapper.updateById(updateObj);
    }

    @Override
    public void deleteList(Long id) {
        // 校验存在
        validateListExists(id);
        // 删除
        listMapper.deleteById(id);
    }

    private void validateListExists(Long id) {
        if (listMapper.selectById(id) == null) {
            throw exception(LIST_NOT_EXISTS);
        }
    }

    @Override
    public GameListDO getList(Long id) {
        return listMapper.selectById(id);
    }

    @Override
    public PageResult<GameListDO> getListPage(GameListPageReqVO pageReqVO) {
        return listMapper.selectPage(pageReqVO);
    }

    @Override
    public List<GameListDO> getList(GameListPageReqVO pageReqVO) {
        LambdaQueryWrapper<GameListDO> wrapper  = new LambdaQueryWrapper<GameListDO>();

        if (pageReqVO.getRecommend() != null && !pageReqVO.getRecommend().equals("")) {
            wrapper.eq(GameListDO::getRecommend, pageReqVO.getRecommend());
        }
        if (pageReqVO.getGameManufacturer() != null && !pageReqVO.getGameManufacturer().equals("")) {
            wrapper.eq(GameListDO::getGameManufacturer, pageReqVO.getGameManufacturer());
        }
        if (pageReqVO.getLimit() != null && !pageReqVO.getLimit().equals("")) {
            wrapper.last(" limit " + pageReqVO.getLimit());
        }

        wrapper.eq(GameListDO::getDeleted, 0);

        return listMapper.selectList(wrapper);
    }

}